Conditions | 25 |
Paths | 15 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like stateGetter.js ➔ stateGetter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
13 | export const stateGetter = (state, props, key, entry) => { |
||
14 | |||
15 | if (props |
||
16 | && props.reducerKeys |
||
17 | && Object.keys(props.reducerKeys).length > 0 |
||
18 | && props.reducerKeys[key]) { |
||
19 | |||
20 | const dynamicKey = props.reducerKeys[key]; |
||
21 | const dynamicState = get(state, dynamicKey, entry); |
||
22 | |||
23 | return dynamicState && dynamicState.toJS |
||
24 | ? dynamicState.toJS() |
||
25 | : dynamicState; |
||
26 | } |
||
27 | |||
28 | const val = get(state, key, entry); |
||
29 | |||
30 | if (val) { |
||
31 | return val.toJS ? val.toJS() : val; |
||
32 | } |
||
33 | |||
34 | return null; |
||
35 | }; |
||
36 | |||
37 | export const get = (state, key, entry) => state |
||
38 | && state[key] |
||
39 | && state[key].get |
||
40 | && state[key].get(entry) |
||
41 | ? state[key].get(entry) |
||
42 | : null; |
||
43 |